/**
 * 當我們的 Bean 有 implement HttpSessionBindingListener 時, 在什麼情況下會去執行 method valueUnbound()<br>
 * 1. 階段閒過久, 使用者的 session 物件會被刪除, 此時, Bean 物件也會從 session 物件移出。<br>
 * 2. 在 Servlet、Scriptlet 或其它 Java 程式中直接將 Bean 物件從 session 物件中移除時。
 */

package basic;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.e885.log.E885Logger;
import common.MyBean;

/**
 * url:http://127.0.0.1:8080/JSPSessionBindingListener/basic<br>
 * <br>
 * 當我們使用 session.setAttribute(), 這時因為 BindingBean 有 implement HttpSessionBindingListener, 所以會去執行 method valueBound()<br>
 * 相對的, 我們用 session.removeAttribute(), 這時因為 BindingBean 有 implement HttpSessionBindingListener, 所以會去執行 method valueUnbound()<br>
 * 
 * @author Eric.Kao(gau168.gau@msa.hinet.net)
 * @version 1.0
 * @company E885 Group
 * @copyright Copyright (c) 2001-2008
 */

public class BasicServlet extends HttpServlet
{
	private static final long serialVersionUID = -9148344502309615401L;

	private String name = "eric.kao";

	protected void service( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException
	{
		HttpSession session = request.getSession( true );

		MyBean bindingBean = new MyBean();
		bindingBean.setName( name );

		// 測試將執行 BindingBean.valueBound()
		E885Logger.testLog.debug( "================== start binding ===================" );
		E885Logger.testLog.debug( "We will bind BindingBean into Session" );
		session.setAttribute( "test", bindingBean );
		E885Logger.testLog.debug( "================== end binding ===================" );

		// 測試將執行 BindingBean.valueUnbound()
		E885Logger.testLog.debug( "================== start to remove BindingBean form Session ===================" );
		E885Logger.testLog.debug( "We will remove BindingBean from Session" );
		session.removeAttribute( "test" );
		E885Logger.testLog.debug( "================== end to remove BindingBean form Session ===================" );
	}
}
